package com.monitorRoutine.monitorRoutineServer.service.impl;

import com.alibaba.fastjson.JSON;
import com.monitorRoutine.monitorRoutineServer.entity.ComputerIp;
import com.monitorRoutine.monitorRoutineServer.entity.MonitorComputerProcess;
import com.monitorRoutine.monitorRoutineServer.entity.MonitorProcessResult;
import com.monitorRoutine.monitorRoutineServer.entity.Process;
import com.monitorRoutine.monitorRoutineServer.mapper.ComputerIpMapper;
import com.monitorRoutine.monitorRoutineServer.mapper.MonitorComputerProcessMapper;
import com.monitorRoutine.monitorRoutineServer.mapper.MonitorProcessResultMapper;
import com.monitorRoutine.monitorRoutineServer.mapper.ProcessMapper;
import com.monitorRoutine.monitorRoutineServer.service.ProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    ProcessMapper processMapper;
    @Autowired
    ComputerIpMapper computerIpMapper;
    @Autowired
    MonitorComputerProcessMapper monitorComputerProcessMapper;
    @Autowired
    MonitorProcessResultMapper monitorProcessResultMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public void scanProcess() {
        List<ComputerIp> computerIps = computerIpMapper.selectAll();
        for (ComputerIp computerIp : computerIps) {
            String redisKey=computerIp.getName()+"_process";
            List<MonitorComputerProcess> monitorComputerProcesses = monitorComputerProcessMapper.selectByComputerName(computerIp.getName());
            if (monitorComputerProcesses.size()<=0) {
                redisTemplate.delete(redisKey);
                continue;
            }
            Collection<String> collect = monitorComputerProcesses.stream().map(monitorComputerProcess -> {
                return JSON.toJSONString(monitorComputerProcess);
            }).collect(Collectors.toList());


            redisTemplate.delete(redisKey);
            redisTemplate.opsForList().rightPushAll(redisKey,collect);
        }

    }

    @Override
    public void sanMonitorProcessResult() {
        List<ComputerIp> computerIps = computerIpMapper.selectAll();
        ArrayList<MonitorProcessResult> monitorProcessResults = new ArrayList<>();
        for (ComputerIp computerIp : computerIps) {
            String redisKey=computerIp.getName()+"_processResult";
            Map<Object, Object> rawEntries = redisTemplate.opsForHash().entries(redisKey);
            if (rawEntries.size()<=0) {
                continue;
            }
            // 删除整个Hash
            redisTemplate.delete(redisKey);
            Map<String, String> entries = rawEntries.entrySet().stream()
                    .collect(Collectors.toMap(
                            e -> (String) e.getKey(),
                            e -> (String) e.getValue()));
            for (String value : entries.values()) {
                MonitorProcessResult monitorProcessResult = JSON.parseObject(value, MonitorProcessResult.class);
                monitorProcessResults.add(monitorProcessResult);
            }
        }
        monitorProcessResultMapper.batchInsert(monitorProcessResults);

    }
}
